package com.burke.gwt.client;

import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;

public class PersonGroup {

	  final double width;
	  final double height;
	  final int numPersons;
	  Image personImg;
	  Image personJumpImg;
	  Person[] persons;
	  boolean imageLoaded = false;
	  public Person targetPerson = null;
	  
	  
	  double k;
	  
	  public PersonGroup(double width, double height, int numPersons) {
	    this.width = width;
	    this.height = height;
	    this.numPersons = numPersons;
	
	    persons = new Person[numPersons];

	    // My image loading is a hack
	    personImg = new Image("../guy-blue.png");
	    personJumpImg = new Image("../guy-blue-armsup.png");//jump
	    personImg.addLoadHandler(new LoadHandler() {
	      public void onLoad(LoadEvent event) {
	        imageLoaded = true;
	        
	        ImageElement imageElement = (ImageElement) personImg.getElement().cast();
	        ImageElement jumpImageElement = (ImageElement) personJumpImg.getElement().cast();
	        
	        //this.onImg = new Image("../google_android.png").getElement().cast();
			//this.offImg = new Image("../ball-d9d9d9.png").getElement().cast();
	        
	        double dx = PersonGroup.this.width / persons.length;
	        double iw = personImg.getWidth();
	        for (int i = persons.length - 1; i >= 0; i--) {
	          Person person = new Person(imageElement, jumpImageElement);
	          
	          person.pos.x = iw + i * dx;//PersonGroup.this.width / 2;
	          
	          person.pos.y = PersonGroup.this.height - imageElement.getHeight();
	          
	          person.targetRect = new Rect(person.pos.x, person.pos.y, person.pos.x + imageElement.getWidth(), person.pos.y + imageElement.getHeight());
	          
	          
	          person.state = 0;
	          if (i==3) { 
	        	  person.state = 1;
	        	  targetPerson = person;
	          }
	          persons[i] = person;
	        }
	      }
	    });
	    personImg.setVisible(false);
	    RootPanel.get().add(personImg); // image must be on page to fire load
	  }
	  
	  public void update(double mouseX, double mouseY) {
		  for (int i = persons.length - 1; i >= 0; i--) {
			  
			  if (persons[i].targetRect.isInside(mouseX, mouseY)) {
				  if (persons[i] != targetPerson) {
					  targetPerson.state = 0; // standing
					  persons[i].state = 1; // jumping
					  targetPerson = persons[i];
				  }
				  
			  }

				  
			  persons[i].update();
		  }
	  }
	  
	  public void draw(Context2d context) {
	    if (!imageLoaded) {
	      return;
	    }
	    
	    for (int i = numPersons - 1; i >= 0; i--) {
	      persons[i].draw(context);
	    }
	  }
	  
	  
	  
	}

